package com.leetcode.周赛.第85场;

/**
 * @author: xiaomi
 * @date: 2021/1/20
 * @description: 836. 矩形重叠
 * https://leetcode-cn.com/problems/rectangle-overlap/
 * 85场
 * https://leetcode-cn.com/contest/weekly-contest-85/
 */
public class A_836_矩形重叠 {

    public static void main(String[] args) {

    }

    /**
     * 得出两个矩形的四个顶点，判断是否存在顶点在另一个矩形的范围内
     * --
     * 考虑不全面 + 如果要正向去枚举就太麻烦了！！！
     *
     * @param rec1
     * @param rec2
     * @return
     */
    public boolean isRectangleOverlapFail(int[] rec1, int[] rec2) {
        if (rec1[0] == rec1[2] || rec1[1] == rec1[3] || rec2[0] == rec2[2] || rec2[1] == rec2[3]) {
            return false;
        }
        int colLeft1 = rec1[0];
        int colRight1 = rec1[2];
        int rowUp1 = rec1[3];
        int rowDown1 = rec1[1];

        int colLeft2 = rec2[0];
        int colRight2 = rec2[2];
        int rowUp2 = rec2[3];
        int rowDown2 = rec2[1];

        if ((colLeft1 <= colLeft2 && colLeft2 < colRight1 && rowUp2 > rowDown1 && rowUp2 <= rowUp1)
                || (colRight2 <= colRight1 && colRight2 > colLeft1 && rowUp2 > rowDown1 && rowUp2 <= rowUp1)
                || (colLeft2 >= colLeft1 && colLeft2 < colRight1 && rowDown2 >= rowDown1 && rowDown2 < rowUp1)
                || (colRight2 <= colRight1 && colRight2 > colLeft1 && rowDown2 >= rowDown1 && rowDown2 < rowUp1)) {
            return true;
        }
        return false;
    }

    /**
     * 考虑 两个矩形的 x 轴和 y 轴的相交情况
     * @param rec1
     * @param rec2
     * @return
     */
    public boolean isRectangleOverlap(int[] rec1, int[] rec2){
        if (rec1[0]>=rec2[2]||rec2[0]>=rec1[2]){
            return false;
        }
        if (rec1[1]>=rec2[2]||rec2[1]>=rec1[2]){
            return false;
        }
        return true;
    }
}
